package com.controller;


import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.net.URLEncoder;
import java.util.Set;

@Controller
public class FileController {
    //向文件上传页面跳转
    @GetMapping("/toUpload")
    public String toUpload(){return "upload";}
    //文件上传管理
    @PostMapping("/uploadFile")
    public String uploadFile(MultipartFile[] fileUpload, Model model){
        //默认文件上传成功，并返回状态信息
        model.addAttribute("uploadStatus","上传成功！");
        for (MultipartFile file:fileUpload){
            //获取文件名以及后缀名
            String fileName=file.getOriginalFilename();
            //指定上传文件本地存储目录，不存在需要提前创建
            String dirPath ="D:/file/";
            File filePath=new File(dirPath);
            if (!filePath.exists()){
                filePath.mkdirs();
            }
            try {
                file.transferTo(new File(dirPath+fileName));
            }catch (Exception e){
                e.printStackTrace();
                //上传失败，返回失败信息
                model.addAttribute("uploadStatus","上传失败"+e.getMessage());

            }
        }
        //携带上传状态信息回调到文件上传页面
        return "upload";
    }
    //向文件下载页面跳转
    @GetMapping("/toDownload")
    public String toDownload(){
        return "download";
    }
    //所有类型文件下载管理
    @GetMapping("/download")
    public ResponseEntity<byte[]>fileDownload(HttpServletRequest request,String filename)throws Exception{
        //指定要下载的文件根路径
        String dirPath="D:/file/";File file=new File(dirPath+File.separator+filename);
        //创建响应头
        HttpHeaders headers=new HttpHeaders();
        //通知浏览器以下方式打开（下载前对文件进行转码）
        filename=getFilename(request,filename);
        headers.setContentDispositionFormData("attachment",filename);
        //定义以流的形式下载返回文件数据
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        try {
            return new ResponseEntity<>(FileUtils.readFileToByteArray(file),headers, HttpStatus.OK);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseEntity<byte[]>(e.getMessage().getBytes(),HttpStatus.EXPECTATION_FAILED);
        }
    }
    //根据浏览器的不同进行编码设置，返回编码后的文件名
    private String getFilename(HttpServletRequest request,String filename)throws Exception{
        //IE不同版本User-Agent中出现的关键词
        String[] IEBrowaserKeyWords={"MSIE","Trident","EDge"};
        //获取请求头代理信息
        String userAgent=request.getHeader("User-Agent");
        for (String keyWord : IEBrowaserKeyWords){
            if (userAgent.contains(keyWord)){
                //IE内核浏览器，统一为UTF-8编码显示，并对转换的+进行更正
                return URLEncoder.encode(filename,"UTF-8").replace("+"," ");
            }
        }
        //火狐登其他浏览器统一为ISO-8859-1编码显示
        return new String(filename.getBytes("UTF-8"),"ISO-8859-1");
    }
}
